package topin.facecat.FaceCat.UI

import facecat.topin.core.FCColor
import facecat.topin.core.FCFont
import facecat.topin.core.FCPaint
import facecat.topin.core.FCRect
import facecat.topin.core.FCTran
import facecat.topin.core.FCView
import topin.facecat.FaceCat.UI.FCDraw.drawText
import topin.facecat.FaceCat.UI.FCDraw.drawUnderLineNum
import topin.facecat.FaceCat.UI.FCDraw.drawUnderLineNum2
import topin.facecat.FaceCat.UI.FCDraw.getPriceColor
import topin.facecat.FaceCat.Service.Security
import topin.facecat.FaceCat.Service.SecurityLatestData


class LatestDiv : FCView() {
    var m_digit = 2
    var latestData = SecurityLatestData()
        private set
    var m_lV2 = false
    var m_code: String? = null
    fun setCode(value: String?) {
        m_code = value
        val security = Security()
        m_isIndex = if (DataCenter.m_securityService!!.getSecurityByCode(m_code!!, security)) {
            security.m_type === 0
        } else {
            false
        }
    }

    var m_isIndex = false
    var m_sName: String? = null

    init {
        borderColor = FCColor.None
    }

    fun drawValue(
        paint: FCPaint,
        value: Double,
        digit: Int,
        font: FCFont?,
        color: Long,
        x: Int,
        y: Int
    ) {
        if (value == latestData.m_upperLimit) {
            val pSize = paint.textSize(FCTran.getValueByDigit(value, digit), font, -1)
            val bkRect = FCRect(x, y, x + pSize.cx, y + pSize.cy)
            bkRect.right += 1
            paint.fillRect(FCColor.rgb(255, 82, 82), bkRect)
            drawUnderLineNum2(paint, value, digit, font, FCColor.Back, true, x, y)
        } else if (value == latestData.m_lowerLimit) {
            val pSize = paint.textSize(FCTran.getValueByDigit(value, digit), font, -1)
            val bkRect = FCRect(x, y, x + pSize.cx, y + pSize.cy)
            bkRect.right += 1
            paint.fillRect(FCColor.rgb(82, 255, 82), bkRect)
            drawUnderLineNum2(paint, value, digit, font, FCColor.Back, true, x, y)
        } else {
            drawUnderLineNum2(paint, value, digit, font, color, true, x, y)
        }
    }

    override fun onPaintForeground(paint: FCPaint, clipRect: FCRect) {
        val width = width
        val height = height
        if (width > 0 && height > 0) {
            val font = FCFont("Default", 14f, false, false, false)
            val lfont = FCFont("Default", 12f, false, false, false)
            val wordColor = MyColor.USERCOLOR218
            var top = 32
            var step = 20
            if (m_isIndex) {
                font.m_fontSize = 14f
                drawText(paint, "现价", wordColor, font, 1, top)
                drawText(paint, "幅度", wordColor, font, 1, top + 20)
                drawText(paint, "总量", wordColor, font, 1, top + 40)
                drawText(paint, "总额", wordColor, font, 1, top + 60)
                drawText(paint, "开盘", wordColor, font, 110, top)
                drawText(paint, "最高", wordColor, font, 110, top + 20)
                drawText(paint, "最低", wordColor, font, 110, top + 40)
                drawText(paint, "振幅", wordColor, font, 110, top + 60)
                //画股票代码
                var yellowColor = FCColor.rgb(230, 230, 230)
                if (MyColor.m_style === 1) {
                    yellowColor = FCColor.Text
                }
                if (latestData.m_code != null && latestData.m_code.length > 0) {
                    var color: Long = 0
                    var close = latestData.m_close
                    val open = latestData.m_open
                    val high = latestData.m_high
                    val low = latestData.m_low
                    var lastClose = latestData.m_lastClose
                    if (close == 0.0) {
                        if (latestData.m_buyPrice1 > 0) {
                            close = latestData.m_buyPrice1
                        } else if (latestData.m_sellPrice1 > 0) {
                            close = latestData.m_sellPrice1
                        }
                    }
                    if (lastClose == 0.0) {
                        lastClose = close
                    }
                    color = getPriceColor(close, lastClose)
                    drawUnderLineNum(paint, close, m_digit, font, color, true, 35, top)
                    //升跌
                    var sub = 0.0
                    if (close == 0.0) {
                        sub = latestData.m_buyPrice1 - lastClose
                        var rate = 100 * (latestData.m_buyPrice1 - lastClose) / lastClose
                        if (latestData.m_buyPrice1 === 0.0) {
                            rate = 0.0
                            sub = 0.0
                        }
                        color = getPriceColor(rate, 0.0)
                        val pleft =
                            drawUnderLineNum(paint, rate, 2, font, color, false, 35, top + 20)
                        drawText(paint, "%", color, font, pleft + 37, top + 20)
                    } else {
                        color = getPriceColor(close, lastClose)
                        sub = close - latestData.m_lastClose
                        val rate = 100 * (close - lastClose) / lastClose
                        val pleft =
                            drawUnderLineNum(paint, rate, 2, font, color, false, 35, top + 20)
                        drawText(paint, "%", color, font, pleft + 37, top + 20)
                    }
                    color = getPriceColor(open, lastClose)
                    drawUnderLineNum(paint, open, m_digit, font, color, true, 145, top)
                    var volume = latestData.m_volume / 100
                    var unit = ""
                    if (volume > 10000000) {
                        volume /= 10000000.0
                        unit = "亿"
                    } else if (volume > 10000) {
                        volume /= 10000.0
                        unit = "万"
                    }
                    var unit2 = ""
                    val amount = latestData.m_amount / 100000000
                    unit2 = "亿"
                    //总手
                    var cleft = drawUnderLineNum(
                        paint,
                        volume,
                        if (unit.length > 0) m_digit else 0,
                        font,
                        yellowColor,
                        true,
                        35,
                        top + 40
                    )
                    if (unit.length > 0) {
                        drawText(paint, unit, yellowColor, font, cleft + 37, top + 40)
                    }
                    color = getPriceColor(high, lastClose)
                    drawUnderLineNum(paint, high, m_digit, font, color, true, 145, top + 20)
                    cleft = drawUnderLineNum(
                        paint,
                        amount,
                        if (unit2.length > 0) m_digit else 0,
                        font,
                        yellowColor,
                        true,
                        35,
                        top + 60
                    )
                    if (unit2.length > 0) {
                        drawText(paint, unit2, yellowColor, font, cleft + 37, top + 60)
                    }
                    color = getPriceColor(low, lastClose)
                    drawUnderLineNum(paint, low, m_digit, font, color, true, 145, top + 40)
                    var range = 0.0
                    if (lastClose != 0.0) {
                        range =
                            100 * (latestData.m_high - lastClose) / lastClose - 100 * (latestData.m_low - lastClose) / lastClose
                    }
                    cleft =
                        drawUnderLineNum(paint, range, 2, font, yellowColor, true, 145, top + 60)
                    if (range > 0) {
                        drawText(paint, "%", yellowColor, font, cleft + 147, top + 60)
                    }
                }
            } else {
                //画买卖盘
                drawText(paint, "卖", wordColor, font, 1, if (m_lV2) 87 else 47)
                drawText(paint, "盘", wordColor, font, 1, if (m_lV2) 140 else 100)
                drawText(paint, "买", wordColor, font, 1, if (m_lV2) 267 else 147)
                drawText(paint, "盘", wordColor, font, 1, if (m_lV2) 310 else 200)
                var buySellStr = "5,4,3,2,1,1,2,3,4,5"
                if (m_lV2) {
                    step = 16
                    buySellStr = "总卖量,10,9,8,7,6,$buySellStr,6,7,8,9,10,总买量"
                    font.m_fontSize = 14f
                }
                val buySellStrs = buySellStr.split("[,]".toRegex()).dropLastWhile { it.isEmpty() }
                    .toTypedArray()
                val strsSize = buySellStrs.size
                for (i in 0 until strsSize) {
                    drawText(paint, buySellStrs[i], wordColor, font, 25, top)
                    top += step
                }
                font.m_fontSize = 14f
                top = if (m_lV2) 390 else 232
                drawText(paint, "现价", wordColor, font, 1, top)
                drawText(paint, "幅度", wordColor, font, 1, top + 20)
                drawText(paint, "总量", wordColor, font, 1, top + 40)
                drawText(paint, "总额", wordColor, font, 1, top + 60)
                drawText(paint, "换手", wordColor, font, 1, top + 80)
                drawText(paint, "PE", wordColor, font, 1, top + 100)
                drawText(paint, "股本", wordColor, font, 1, top + 120)
                drawText(paint, "流通", wordColor, font, 1, top + 140)
                drawText(paint, "均价", wordColor, font, 110, top)
                drawText(paint, "开盘", wordColor, font, 110, top + 20)
                drawText(paint, "最高", wordColor, font, 110, top + 40)
                drawText(paint, "最低", wordColor, font, 110, top + 60)
                drawText(paint, "振幅", wordColor, font, 110, top + 80)
                drawText(paint, "量比", wordColor, font, 110, top + 100)
                drawText(paint, "市值", wordColor, font, 110, top + 120)
                drawText(paint, "流值", wordColor, font, 110, top + 140)
                //画股票代码
                var yellowColor = FCColor.rgb(190, 190, 235)
                if (MyColor.m_style === 1) {
                    yellowColor = FCColor.Text
                }
                if (latestData.m_code != null && latestData.m_code.length > 0) {
                    var close = latestData.m_close
                    val open = latestData.m_open
                    val high = latestData.m_high
                    val low = latestData.m_low
                    var lastClose = latestData.m_lastClose
                    if (close == 0.0) {
                        if (latestData.m_buyPrice1 > 0) {
                            close = latestData.m_buyPrice1
                        } else if (latestData.m_sellPrice1 > 0) {
                            close = latestData.m_sellPrice1
                        }
                    }
                    if (lastClose == 0.0) {
                        lastClose = close
                    }
                    val plist = ArrayList<Double>()
                    val vlist = ArrayList<Double>()
                    val multi = 100.0
                    plist.add(latestData.m_sellPrice5)
                    plist.add(latestData.m_sellPrice4)
                    plist.add(latestData.m_sellPrice3)
                    plist.add(latestData.m_sellPrice2)
                    plist.add(latestData.m_sellPrice1)
                    vlist.add(latestData.m_sellVolume5 / multi)
                    vlist.add(latestData.m_sellVolume4 / multi)
                    vlist.add(latestData.m_sellVolume3 / multi)
                    vlist.add(latestData.m_sellVolume2 / multi)
                    vlist.add(latestData.m_sellVolume1 / multi)
                    plist.add(latestData.m_buyPrice1)
                    plist.add(latestData.m_buyPrice2)
                    plist.add(latestData.m_buyPrice3)
                    plist.add(latestData.m_buyPrice4)
                    plist.add(latestData.m_buyPrice5)
                    vlist.add(latestData.m_buyVolume1 / multi)
                    vlist.add(latestData.m_buyVolume2 / multi)
                    vlist.add(latestData.m_buyVolume3 / multi)
                    vlist.add(latestData.m_buyVolume4 / multi)
                    vlist.add(latestData.m_buyVolume5 / multi)
                    var color: Long = 0
                    var mx = 0.0
                    for (i in vlist.indices) {
                        if (i == 0) {
                            mx = vlist[i]
                        } else {
                            if (mx < vlist[i]) {
                                mx = vlist[i]
                            }
                        }
                    }
                    font.m_fontSize = (if (m_lV2) 14 else 14).toFloat()
                    if (mx > 0) {
                        //绘制买卖盘
                        val pLength = plist.size
                        top = 32
                        if (m_lV2) {
                            top += step
                        }
                        for (i in 0 until pLength) {
                            color = getPriceColor(plist[i], lastClose)
                            drawValue(
                                paint,
                                plist[i], m_digit, font, color, if (m_lV2) 80 else 60, top
                            )
                            color = if (i < 5) {
                                getPriceColor(0.5, 1.0)
                            } else {
                                getPriceColor(1.0, 0.5)
                            }
                            drawUnderLineNum2(
                                paint,
                                vlist[i], 0, font, color, true, if (m_lV2) 130 else 110, top
                            )
                            //paint.fillRect(color, new FCRect(width - (int)(vlist[i] / mx * 50), top + step / 2 - 2, width, top + step / 2 + 2));
                            top += step
                        }
                        if (m_lV2) {
                            top += step
                        }
                    }
                    vlist.clear()
                    plist.clear()
                    top = if (m_lV2) 390 else 232
                    //成交
                    color = getPriceColor(close, lastClose)
                    drawValue(paint, close, m_digit, font, color, 35, top)
                    //均价
                    if (latestData.m_volume > 0) {
                        val avgPrice = latestData.m_amount / latestData.m_volume
                        color = getPriceColor(avgPrice, lastClose)
                        drawValue(paint, avgPrice, m_digit, font, color, 145, top)
                    } else {
                        drawText(paint, "-", wordColor, font, 145, top)
                    }
                    //升跌
                    var sub = 0.0
                    if (close == 0.0) {
                        sub = latestData.m_buyPrice1 - lastClose
                        var rate = 100 * (latestData.m_buyPrice1 - lastClose) / lastClose
                        if (latestData.m_buyPrice1 === 0.0) {
                            rate = 0.0
                            sub = 0.0
                        }
                        color = getPriceColor(rate, 0.0)
                        val pleft =
                            drawUnderLineNum(paint, rate, 2, font, color, false, 35, top + 20)
                        drawText(paint, "%", color, font, pleft + 37, top + 20)
                    } else {
                        color = getPriceColor(close, lastClose)
                        sub = close - latestData.m_lastClose
                        val rate = 100 * (close - lastClose) / lastClose
                        val pleft =
                            drawUnderLineNum(paint, rate, 2, font, color, false, 35, top + 20)
                        drawText(paint, "%", color, font, pleft + 37, top + 20)
                    }
                    color = getPriceColor(open, lastClose)
                    drawValue(paint, open, m_digit, font, color, 145, top + 20)
                    var volume = latestData.m_volume / 100
                    var unit = ""
                    if (volume > 10000000) {
                        volume /= 10000000.0
                        unit = "亿"
                    } else if (volume > 10000) {
                        volume /= 10000.0
                        unit = "万"
                    }
                    var unit2 = ""
                    val amount = latestData.m_amount / 100000000
                    unit2 = "亿"
                    //总手
                    var cleft = drawUnderLineNum(
                        paint,
                        volume,
                        if (unit.length > 0) m_digit else 0,
                        font,
                        yellowColor,
                        true,
                        35,
                        top + 40
                    )
                    if (unit.length > 0) {
                        drawText(paint, unit, yellowColor, font, cleft + 37, top + 40)
                    }
                    color = getPriceColor(high, lastClose)
                    drawValue(paint, high, m_digit, font, color, 145, top + 40)
                    cleft = drawUnderLineNum(
                        paint,
                        amount,
                        if (unit2.length > 0) m_digit else 0,
                        font,
                        yellowColor,
                        true,
                        35,
                        top + 60
                    )
                    if (unit2.length > 0) {
                        drawText(paint, unit2, yellowColor, font, cleft + 37, top + 60)
                    }
                    color = getPriceColor(low, lastClose)
                    drawValue(paint, low, m_digit, font, color, 145, top + 60)
                    val turnoverRate = latestData.m_turnoverRate
                    cleft = drawUnderLineNum(
                        paint,
                        turnoverRate,
                        2,
                        font,
                        yellowColor,
                        false,
                        35,
                        top + 80
                    )
                    drawText(paint, "%", yellowColor, font, cleft + 37, top + 80)
                    var range = 0.0
                    if (lastClose != 0.0) {
                        range =
                            100 * (latestData.m_high - lastClose) / lastClose - 100 * (latestData.m_low - lastClose) / lastClose
                    }
                    cleft =
                        drawUnderLineNum(paint, range, 2, font, yellowColor, true, 145, top + 80)
                    if (range > 0) {
                        drawText(paint, "%", yellowColor, font, cleft + 147, top + 80)
                    }
                    if (latestData.m_pe < 0) {
                        drawText(paint, "亏损", yellowColor, font, 35, top + 100)
                    } else {
                        drawUnderLineNum(
                            paint,
                            latestData.m_pe,
                            m_digit,
                            font,
                            yellowColor,
                            true,
                            35,
                            top + 100
                        )
                    }
                    drawUnderLineNum(
                        paint,
                        latestData.m_ratio,
                        m_digit,
                        font,
                        yellowColor,
                        true,
                        145,
                        top + 100
                    )
                    var equity = latestData.m_equity
                    if (equity > 100000000) {
                        equity /= 100000000.0
                        unit = "亿"
                    } else if (equity > 10000) {
                        equity /= 10000.0
                        unit = "万"
                    }
                    cleft = drawUnderLineNum(
                        paint,
                        equity,
                        if (unit.length > 0) m_digit else 0,
                        font,
                        yellowColor,
                        true,
                        35,
                        top + 120
                    )
                    if (unit.length > 0) {
                        drawText(paint, unit, yellowColor, font, cleft + 37, top + 120)
                    }
                    var flowEquity = latestData.m_flowEquity
                    if (flowEquity > 100000000) {
                        flowEquity /= 100000000.0
                        unit = "亿"
                    } else if (flowEquity > 10000) {
                        flowEquity /= 10000.0
                        unit = "万"
                    }
                    cleft = drawUnderLineNum(
                        paint,
                        flowEquity,
                        if (unit.length > 0) m_digit else 0,
                        font,
                        yellowColor,
                        true,
                        35,
                        top + 140
                    )
                    if (unit.length > 0) {
                        drawText(paint, unit, yellowColor, font, cleft + 37, top + 140)
                    }
                    cleft = drawUnderLineNum(
                        paint,
                        latestData.m_marketValue / 100000000,
                        2,
                        font,
                        yellowColor,
                        true,
                        145,
                        top + 120
                    )
                    val unit3 = "亿"
                    if (unit3.length > 0) {
                        drawText(paint, unit3, yellowColor, font, cleft + 147, top + 120)
                    }
                    cleft = drawUnderLineNum(
                        paint,
                        latestData.m_flow / 100000000,
                        2,
                        font,
                        yellowColor,
                        true,
                        145,
                        top + 140
                    )
                    if (unit3.length > 0) {
                        drawText(paint, unit3, yellowColor, font, cleft + 147, top + 140)
                    }
                }
                paint.drawLine(FCColor.Border, 1f, 0, 0, 0, 0, height)
                paint.drawLine(FCColor.Border, 1f, 0, 0, 30, width, 30)
                paint.drawLine(FCColor.Border, 1f, 0, 24, 30, 24, top - 2)
                paint.drawLine(FCColor.Border, 1f, 0, 0, 131, width, 131)
                paint.drawLine(FCColor.Border, 1f, 0, 0, top - 2, width, top - 2)
                paint.drawLine(FCColor.Border, 1f, 0, width, 0, width, height)
            }
            font.m_bold = false
            font.m_fontSize = 14f
            //股票代码
            if (m_code != null && m_code!!.length > 0) {
                val showCode = m_code!!.substring(0, m_code!!.indexOf("."))
                drawText(paint, showCode, MyColor.USERCOLOR218, font, 5, 4)
            }
            //股票名称
            if (m_sName != null && m_sName!!.length > 0) {
                drawText(paint, m_sName, MyColor.USERCOLOR217, font, 55, 4)
            }
        }
    }

    fun bindData(latestData: SecurityLatestData, refresh: Boolean) {
        this.latestData = latestData
        setCode(latestData.m_code)
        val security = Security()
        m_sName =
            if (DataCenter.m_securityService!!.getSecurityByCode(latestData.m_code, security)) {
                security.m_name
            } else {
                ""
            }
        if (refresh) {
            invalidate()
        }
    }
}


